#!/bin/bash

echo "安装新版价格策略应用的依赖包..."

# 检查Python环境
if command -v python3 &> /dev/null; then
    PYTHON_CMD="python3"
elif command -v python &> /dev/null; then
    PYTHON_CMD="python"
else
    echo "错误: 未找到Python环境"
    exit 1
fi

echo "使用Python命令: $PYTHON_CMD"

# 安装基础依赖
echo "安装基础依赖包..."
$PYTHON_CMD -m pip install --upgrade pip

# 安装requirements.txt中的依赖
if [ -f "requirements.txt" ]; then
    echo "从requirements.txt安装依赖..."
    $PYTHON_CMD -m pip install -r requirements.txt
else
    echo "requirements.txt不存在，手动安装依赖..."
    $PYTHON_CMD -m pip install streamlit pandas numpy matplotlib seaborn sqlalchemy gurobipy loguru openpyxl scipy
fi

# 检查关键依赖是否安装成功
echo "检查依赖安装状态..."
$PYTHON_CMD -c "
import sys
packages = ['streamlit', 'pandas', 'numpy', 'matplotlib', 'seaborn', 'sqlalchemy', 'gurobipy', 'loguru']
missing = []
for pkg in packages:
    try:
        __import__(pkg)
        print(f'✅ {pkg}')
    except ImportError:
        print(f'❌ {pkg}')
        missing.append(pkg)

if missing:
    print(f'缺少依赖: {missing}')
    sys.exit(1)
else:
    print('所有依赖安装成功!')
"

if [ $? -eq 0 ]; then
    echo "依赖安装完成！"
    echo "现在可以运行: python new_app.py"
else
    echo "依赖安装失败，请检查错误信息"
    exit 1
fi